//
//  ZJHCaipuDetailViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "ZJHCaipuDetailViewController.h"
#import "FitHeader.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>
#import "ZJHCaiPuMaterialModel.h"
#import "FitHeader.h"
#import "CaiPuMaterialCollectionViewCell.h"
#import "Adaptive.h"
#import "FontSizeSingleTon.h"
#define Font [[FontSizeSingleTon shareFontSize]array]
@interface ZJHCaipuDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong)ZJHCaiPuMaterialModel *materialModel;
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)MBProgressHUD *progressView;
@end

@implementation ZJHCaipuDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    NSLog(@"%ld", _foodId);
    self.title = _name;
}
-(void)loadView
{
    [super loadView];
    [self getData];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREENWIDTH, SCREENHEIGHT - 104);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 104) collectionViewLayout:flowLayout];
    [_collectionView registerClass:[CaiPuMaterialCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [self createProgressView];
    
}
//创建菊花
-(void)createProgressView
{
    _progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"加载中";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    _progressView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_progressView];
    [_progressView show:YES];
}
-(void)getData
{
    NSString *postString = @"http://api.haodou.com/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=1cc2050fd3546d59e182c84229a9fd17&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7CCC2D6423-97F6-437A-B8DE-CBB86A71C6F1%7C608CBAFB-426B-4C02-96CA-F5B029C17D76&format=json&loguid=&method=Info.getFoodInfo&nonce=1446797861&sessionid=1446797812&signmethod=md5&timestamp=1446797861&uuid=f54ccedd4a37e51b4340c5dc18fe353c&v=2&vc=43&vn=v5.4.0";
    NSString *bodyString = [NSString stringWithFormat:@"foodid=%ld&sign=&uid=&uuid=f54ccedd4a37e51b4340c5dc18fe353c", _foodId];
    [HTTPMethod getDataByString:postString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        NSDictionary *dic = [data objectForKey:@"result"];
        _materialModel = [[ZJHCaiPuMaterialModel alloc]init];
        [_materialModel setValuesForKeysWithDictionary:[dic objectForKey:@"info"]];
        
        [_collectionView reloadData];
        [_progressView removeFromSuperview];
    }];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CaiPuMaterialCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    if (![_materialModel.Name isEqualToString:@""]) {
        
        cell.materialModel = _materialModel;
    }
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat contentLableHeight = [Adaptive adaptiveHeightByString:_materialModel.Content Width:295 * wt FontSize:[Font[1] integerValue]];
    CGFloat effectLableHeight = [Adaptive adaptiveHeightByString:_materialModel.Effect Width:350 * wt FontSize:[Font[1] integerValue]];
    CGFloat pickLableHeight = [Adaptive adaptiveHeightByString:_materialModel.Pick Width:350 * wt FontSize:[Font[1] integerValue]];
    CGFloat height = contentLableHeight + effectLableHeight + pickLableHeight + 120 * ht;
    CGSize size = CGSizeMake(SCREENWIDTH, height);
    return size;
}
-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
